﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TwitterPlugin
{
    public partial class TwitterSettings : Form
    {
        public TwitterSettings()
        {
            InitializeComponent();
            LoadSettings();
            SetUiState();
        }

        private void LoadSettings()
        {
            txtUser.Text = Properties.Settings.Default.username;
            txtPassword.Text = Properties.Settings.Default.password;

            chkTemperature.Checked = Properties.Settings.Default.EnableTemperature;
            rdTempAlert.Checked = Properties.Settings.Default.EnableTemperatureAlert;
            
            rdTempDelay.Checked = Properties.Settings.Default.EnableTemperatureDelay;
            txtTempDelay.Enabled = Properties.Settings.Default.EnableTemperatureDelay;
            
            chkPours.Checked = Properties.Settings.Default.EnablePour;
            txtPourString.Text = Properties.Settings.Default.PourString;

            txtTempDelay.Text = Properties.Settings.Default.TemperatureDelay.ToString();
        }

        private void SaveSettings()
        {
            Properties.Settings.Default.username = txtUser.Text;
            Properties.Settings.Default.password = txtPassword.Text;

            Properties.Settings.Default.EnableTemperature = chkTemperature.Checked;
            Properties.Settings.Default.EnableTemperatureAlert = rdTempAlert.Checked;

            Properties.Settings.Default.EnableTemperatureDelay = rdTempDelay.Checked;
            Properties.Settings.Default.EnableTemperatureDelay = txtTempDelay.Enabled;

            Properties.Settings.Default.EnablePour = chkPours.Checked;
            Properties.Settings.Default.PourString = txtPourString.Text;

            Properties.Settings.Default.TemperatureDelay = Convert.ToInt32(txtTempDelay.Text);
        }

        private void SetUiState()
        {
            bool enableTemperature = false;
            bool enableTempDelay = false;
            bool enablePourString = false;
            
            if(chkTemperature.Checked == true)
            {
                enableTemperature = true;
                if(rdTempDelay.Checked == true)
                {
                    enableTempDelay = true;
                }
            }

            if (chkPours.Checked == true)
            {
                enablePourString = true;
            }

            rdTempDelay.Enabled = enableTemperature;
            rdTempAlert.Enabled = enableTemperature;
            txtTempDelay.Enabled = enableTempDelay;
            txtPourString.Enabled = enablePourString;
        }

        private void chkTemperature_CheckedChanged(object sender, EventArgs e)
        {
            SetUiState();
        }

        private void chkPours_CheckedChanged(object sender, EventArgs e)
        {
            SetUiState();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveSettings();
        }

        private void rdTempDelay_CheckedChanged(object sender, EventArgs e)
        {
            SetUiState();
        }


    }
}
